package com.tre.jdevtemplateboot.common.util;

import com.tre.jdevtemplateboot.domain.bo.ProjectPom;
import org.apache.maven.model.Model;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;

/**
 * @author JDev
 */
public class ProjectPomUtils {

    private static final Logger logger = LoggerFactory.getLogger(ProjectPomUtils.class);

    private ProjectPomUtils(){}

    public static ProjectPom queryPom(){

        ProjectPom projectPom = new ProjectPom();
        try{
            String rootPath = System.getProperty("user.dir");
            MavenXpp3Reader reader = new MavenXpp3Reader();
            String myPom = rootPath + File.separator + "pom.xml";
            Model model = reader.read(new FileReader(myPom));
            projectPom.setGroupId(model.getGroupId());
            projectPom.setArtifactId(model.getArtifactId());
            projectPom.setVersion(model.getVersion());
            projectPom.setPackaging(model.getPackaging());
            projectPom.setJavaVersion(model.getProperties().getProperty("java.version"));
            projectPom.setJdevBootCoreVersion(model.getProperties().getProperty("jdev-boot-core.version"));
        }
        catch (IOException | XmlPullParserException ex) {
            logger.error(ex.getMessage());
        }
        return projectPom;
    }

}
